<template>
	<view>
		
	</view>
</template>

<script>
	export default {
		name:"Share",
		data() {
			return {
				share: {
					title: '',//自定义标题
					path: `/pages/index/index`,  //默认跳转首页
					imageUrl: '',  //可设置默认分享图，不设置默认截取头部5:4
				}
			};
		},
		onLoad() {
			uni.setEnableDebug({
			  enableDebug: true
			})
		},
		onShareAppMessage(res) {
			// 动态获取当前页面栈
			let pages = getCurrentPages(); //获取所有页面栈实例列表
			let nowPage = pages[pages.length - 1]; //当前页页面实例
			// let prevPage = pages[pages.length - 2]; //上一页页面实例
			this.share.path = `/${nowPage.route}`;
			
			const { title, path, imageUrl } = this.share;
			
			return {
				provider: "weixin",
				title,
				path,
				imageUrl,
				success: result => {
					console.log(result)
					uni.showToast({
						title: '分享成功'
					})
				},
				fail: err => {
					console.log(err)
					uni.showToast({
						title: "分享失败"
					})
				}
			}
		},
		onShareTimeline(res) {
			// 动态获取当前页面栈
			let pages = getCurrentPages(); //获取所有页面栈实例列表
			let nowPage = pages[pages.length - 1]; //当前页页面实例
			// let prevPage = pages[pages.length - 2]; //上一页页面实例
			this.share.path = `/${nowPage.route}`;
			
			const { title, path, imageUrl } = this.share;
			
			return {
				provider: "weixin",
				title,
				path,
				imageUrl,
				success: result => {
					console.log(result)
					uni.showToast({
						title: '分享成功'
					})
				},
				fail: err => {
					console.log(err)
					uni.showToast({
						title: "分享失败"
					})
				}
			}
		}
	}
</script>

<style>

</style>